package com.lanxin.oa.zu.dao.daoImpl;

import com.lanxin.base.BaseDaoImpl;
import com.lanxin.bean.*;
import com.lanxin.oa.zu.dao.KehuDao;
import com.lanxin.util.PageBean;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class KehuDaoImpl extends BaseDaoImpl implements KehuDao {
    @Override
    public PageBean selectcp(Integer page, Integer pageSize, String linkname) {
        if(page==null){
            page=1;
        }
        if(pageSize==null){
            pageSize=5;
        }
        String hql="from KhProduct cp where 1=1";
        if(linkname !=null && !linkname.equals("")){
            hql = hql + "  and cp.type = '" + linkname + "'  ";
        }
        return super.pageFenYE(hql,page,pageSize);
    }

    @Override
    public KhProduct selectcpName(String linkname,Integer oid) {

        String hql = "from KhProduct cp where cp.linkname='" + linkname + "' ";
        KhProduct cp = null;
        if (oid != null) {
            hql = hql + " and cp.oid=" + oid;
            cp = (KhProduct) selectByHQL(hql);
            if (cp != null) {
                return null;
            }
            cp = (KhProduct) selectByHQL("from KhProduct cp where cp.linkname='" + linkname + "' ");
            if (cp == null) {
                return null;
            }
        } else {
            return (KhProduct) selectByHQL(hql);
        }

        return cp;
    }

    @Override
    public KhProduct selectcpByid(Integer oid) {
        return (KhProduct)super.getById(KhProduct.class,oid);
    }

    @Override
    public KhCustomerfavorite selectgx(Integer oid) {
        return (KhCustomerfavorite)super.selectByHQL("from KhCustomerfavorite khgx where khgx.productid="+oid);
    }

    @Override
    public PageBean selectkh(Integer page, Integer pageSize, String customerName, String phone, Integer companyType, Integer vocation) {
        if(page==null){
            page=1;
        }
        if(pageSize==null){
            pageSize=5;
        }
        String hql="from KhCustomer kh left join fetch kh.xingzhi left join fetch kh.hangye where 1=1";
        if(customerName !=null && !customerName.equals("")){
            hql = hql + "  and kh.customerName = '" + customerName + "'  ";
        }
        if(phone !=null && !phone.equals("")){
            hql = hql + "  and kh.phone = '" + phone + "'  ";
        }
        if(companyType !=null && !companyType.equals("")){
            hql = hql + "  and kh.companyType = '" + companyType + "'  ";
        }
        if(vocation !=null && !vocation.equals("")){
            hql = hql + "  and kh.vocation = '" + vocation + "'  ";
        }
        return super.pageFenYE(hql,page,pageSize);
    }

    @Override
    public List selectxz() {
        return super.queryAll("from JcBasicdatavalue jc where jc.type=7");
    }

    @Override
    public List selecthy() {
        return super.queryAll("from JcBasicdatavalue jc where jc.type=8");
    }

    @Override
    public List selectxy() {
        return super.queryAll("from JcBasicdatavalue jc where jc.type=9");
    }

    @Override
    public KhCustomer selectkhById(Integer oid) {
        return (KhCustomer)super.selectByHQL("from KhCustomer kh left join fetch kh.kulxr left join fetch kh.xingzhi left join fetch kh.hangye left join fetch kh.xinyong  where kh.oid="+oid);
    }

    @Override
    public PageBean selectkhgxcp(Integer customerid, Integer page, Integer pageSize) {
        if(page==null){
            page=1;
        }
        if(pageSize==null){
            pageSize=5;
        }
        String hql="from KhCustomerfavorite gx left join fetch gx.kh left join fetch gx.cp where gx.customerid='"+customerid+"'";
        return super.pageFenYE(hql,page,pageSize);
    }

    @Override
    public List selectcp() {
        return super.queryAll("from KhProduct cp");
    }

    @Override
    public KhCustomerfavorite guanxin(Integer customerid, Integer productid) {
        return (KhCustomerfavorite)super.selectByHQL("from KhCustomerfavorite gx where gx.customerid='"+customerid+"' and gx.productid='"+productid+"'");
    }

    @Override
    public List selectlxr(Integer oid) {
        return queryAll("from KhCustomercontact lxr where lxr.customerid="+oid);
    }

    @Override
    public PageBean selectgzjl(Integer oid,Integer page,Integer pageSize) {
        if(page==null){
            page=1;
        }
        if(pageSize==null){
            pageSize=5;
        }
        String hql="from KhContactrecord gzjl left join fetch gzjl.lxr where gzjl.favoriteid="+oid;
        return super.pageFenYE(hql,page,pageSize);
    }

}
